package ui {
	import flash.display.BlendMode;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	public class Scrollbar extends UIItem {
		
		public var min:Number = 0;
		public var max:Number = 100;
		public var step:Number = 0.25;
		public var value:Number = 0;
		
		private var background:Sprite = new Sprite();
		private var thumb:Sprite = new Sprite();
		private var w:Number;
		private var h:Number = 15;
		private var offset:Point = new Point();
		private var label:TextLabel;
		private var txt:TextLabel = new TextLabel("", { color: 0x7C7C7C });
		private var container:Sprite = new Sprite();
		public function Scrollbar(labelTxt:String, w:Number, options:Object = null) {
			super(options);
			this.w = w;
			
			var g:Graphics;
			
			g = background.graphics;
			//g.lineStyle(1, 0x000000, 0.5, true);
			g.beginFill(0xEAEAEA, 1);
			g.drawRoundRect(0, 0, w, h, 6, 6);
			g.endFill();
			background.addEventListener(MouseEvent.MOUSE_DOWN, backgroundDown);
			addChild(background);
			
			//addChild(txt);
			
			container.mouseChildren = container.mouseEnabled = false;
			addChild(container);
			
			g = thumb.graphics;
			//g.lineStyle(1, 0x000000, 0.3, true);
			g.beginFill(0x4477FD, 1);
			g.drawRoundRect(0, 0, 15, h, 6, 6);
			g.endFill();
			//thumb.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			container.addChild(thumb);
			
			buttonMode = true;
			thumb.tabIndex = nextTabIndex++;
			
			container.addChild(label = new TextLabel(labelTxt, { color: 0x939393 } ));
			container.blendMode = BlendMode.LAYER;
			//thumb.blendMode = BlendMode.LAYER;
			label.blendMode = BlendMode.ADD;
			
			container.addChild(txt);
			txt.blendMode = BlendMode.ADD;
			
			//label.mouseEnabled = label.mouseChildren = txt.mouseEnabled = txt.mouseChildren = false;
			
			addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheel);
			addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			
			txt.x = label.width+3;
			updatePosition(false);
		}
		
		private function keyDown(e:KeyboardEvent):void {
			switch (e.keyCode) {
				case Keyboard.LEFT:
					value -= step;
					updatePosition();
					setTimeout(regainFocus, 0);
					break;
				case Keyboard.RIGHT:
					value += step;
					updatePosition();
					setTimeout(regainFocus, 0);
					break;
			}
		}
		private function regainFocus():void {
			stage.focus = thumb;
		}
		
		private function mouseWheel(e:MouseEvent):void {
			value += e.delta > 0 ? step : -step;
			updatePosition();
		}
		
		private function backgroundDown(e:MouseEvent):void {
			if (thumb.hitTestPoint(stage.mouseX, stage.mouseY, true)) {
				mouseDown(e);
				return;
			}
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			offset.x = thumb.width/2;
			mouseMove();
		}
		
		private function mouseDown(e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			offset.x = thumb.mouseX;
			mouseMove();
		}
		
		private function mouseMove(e:MouseEvent = null):void {
			var tx:Number = mouseX-offset.x;
			value = (min+(max-min)*tx/(w-thumb.width));
			updatePosition();
			if (e) e.updateAfterEvent();
		}
		private function updatePosition(dispatch:Boolean = true):void {
			if (!isNaN(step)) {
				value = Math.round(value/step)*step;
				value = Math.round(value*1e13)/1e13;
			}
			value = Math.max(min, Math.min(max, value));
			thumb.x = (value-min)/(max-min)*(w-thumb.width);
			txt.text = ""+value;
			dispatchEvent(new ValueEvent(ValueEvent.VALUE_CHANGED, value));
			//txt.x = tx < w/2 ? w-txt.width : label.width+2;
		}
		
		private function mouseUp(e:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
		}
		
	}

}